package GenericClass;

import java.awt.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

import Exception.*;

public class FormatScrollPane extends DefaultTableCellRenderer
{
	private static final long serialVersionUID = 1L;

	/**
	 * This method allow you to format you scrollpane automatically with a lot of parameter.
	 * 
	 * Attention : you have to create an empty table. It will be transform in the scrollpane.
	 * 
	 * @param table is the table you have to send to the method
	 * <li>The value is <i>your_JTable</i></li>
	 * @param tableModel is the model of your database which will be shown in the table
	 * <li>The value is <i>your_MyTableModel</i></li>
	 * @param arrayHeader is the arrayList which contains the title of the column
	 * <li>The value is <i>your_ArrayList<String></i></li>
	 * @param scrollPane is the scrollpane you have to send to the method
	 * <li>The value is <i>your_JScrollPane</i></li>
	 * @param selection allows you to have a multiple or a single selection
	 * <li>The value is <i><b>"single"</b></i> or <i><b>"multiple"</b></i></li>
	 * @param rowSorter allows you to not have any problem between view selection and model selection
	 * <li>The value is a boolean : <i><b>true</b></i> or <i><b>false</b></i></li>
	 * @param colorCells allows you to color the cells, which pemit a better facility of reading
	 * <li>The value is a boolean : <i><b>true</b></i> or <i><b>false</b></i></li>
	 * @param autoResize allows you to autoresize the different column depending on the width of the column title
	 * <li>The value is a boolean : <i><b>true</b></i> or <i><b>false</b></i></li>
	 * @param center allows you to center the cells in the scrollpane
	 * <li>The value is a boolean : <i><b>true</b></i> or <i><b>false</b></i></li>
	 * @throws FormatScrollPaneException
	 */
	public void autoFillScrollPane(JTable table, TableModel tableModel, ArrayList<String> arrayHeader, JScrollPane scrollPane, String selection, boolean rowSorter, boolean colorCells, boolean autoResize, boolean center) throws FormatScrollPaneException //Generic Method
	{
		//Initialization
		int cpt, som = 0;
		TableColumn column;
		
		if(rowSorter == true)
			table.setAutoCreateRowSorter(true);
		table.setModel(tableModel); //Set the model in the table
		scrollPane.setViewportView(table); //Set the table int the scrollPane
		
		//Color the cells 1 over 2
		if(colorCells == true)
			for(cpt = 0, column = null; cpt < tableModel.getColumnCount(); cpt++)
			{
				column = table.getColumnModel().getColumn(cpt);
				TableCellRenderer cell = (new FormatScrollPane()); //Special cell
				column.setCellRenderer(cell);
				
				//If center option is ON
				if(center == true)
					((JLabel)cell).setHorizontalAlignment(SwingConstants.CENTER);
			}
		else
			for(cpt = 0, column = null; cpt < tableModel.getColumnCount(); cpt++)
			{
				column = table.getColumnModel().getColumn(cpt);
				NormalTableCellRenderer cell = new NormalTableCellRenderer(); //Normal cell
				column.setCellRenderer(cell);

				//If center option is ON
				if(center == true)
					((JLabel) cell).setHorizontalAlignment(SwingConstants.CENTER);
			}
		
		if(selection.equals("single") == true || selection.equals("SINGLE"))
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		else if(selection.equals("multiple") == true || selection.equals("MULTIPLE"))
			table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		else if(selection.equals("forbidden") == true || selection.equals("FORBIDDEN"))
		{
			table.setRowSelectionAllowed(false);
			table.setColumnSelectionAllowed(false);
		}
		else
		{
			String error = "You have to choose between \"single\" or \"multiple\" or \"forbidden\"";
			throw new FormatScrollPaneException(error);
		}

		//Autoresize the header
		if(autoResize == true)
			if(scrollPane.getWidth() != 0)
			{
				column = null;
				
				for(cpt = 0; cpt < tableModel.getColumnCount(); cpt++)
				{
					column = table.getColumnModel().getColumn(cpt);
					
					if(cpt < arrayHeader.size())
						column.setHeaderValue(arrayHeader.get(cpt));
					else
					{
						column.setMinWidth(0);
						column.setMaxWidth(0);
					}
					som += column.getWidth();
				}
				
				if(som > scrollPane.getWidth())
					table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			}
			else
			{
				String error = "The scrollPane has NOT a valid width !";
				throw new FormatScrollPaneException(error);
			}
	}

	public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
	{
		Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column); //Get the cell renderer
		
		if(isSelected)
			cell.setBackground(table.getSelectionBackground()); //When a cell is selected the background changes
		else
		{
			if(row % 2 == 0)//Color one row over two
				cell.setBackground(new Color(214, 217, 223));
			else
				cell.setBackground(Color.white);
		}
		
		return cell;
	}
	
	private class NormalTableCellRenderer extends DefaultTableCellRenderer
	{
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
		{ 
			Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			return cell;
		}
	}
}